package vn.allmobile.ambnews.loader;

import java.util.ArrayList;

import org.json.JSONObject;

import vn.allmobile.ambnews.model.ArticleInfo;
import vn.allmobile.ambnews.model.ListArticleInfo;
import vn.allmobile.ambnews.ulti.Constant;
import vn.allmobile.ambnews.ulti.Log;
import vn.allmobile.ambnews.ulti.Request;

import android.content.Context;
import android.text.TextUtils;

public class MainPageLoader extends BaseLoader
{

  private String mAppId;
  public boolean mNeedNextPage;
  public int mPage = 1;

    public MainPageLoader(Context context) {
		super(context);	
		this.mNeedNextPage = false;
		Log.LOGTAG = "AppCommentsLoader";
	}

	@Override
	protected BaseResult getResultInstance() {
		 return new Result();
	}
	
	@Override
	protected BaseResult parseResult(JSONObject json, BaseResult result)
			throws Exception {
		MainPageLoader.Result localResult = null;
		if(json != null)
		{
		  localResult = new MainPageLoader.Result();
		  localResult.mCommentTotalCount = json.optInt("commentCount");
          localResult.mScoreTotalCount = json.optInt("pointCount");
          localResult.mScore = json.optDouble("score");
          localResult.mCountOfPoints = new int[5];
          localResult.mCountOfPoints[0] = json.optInt("point1Count");
          localResult.mCountOfPoints[1] = json.optInt("point2Count");
          localResult.mCountOfPoints[2] = json.optInt("point3Count");
          localResult.mCountOfPoints[3] = json.optInt("point4Count");
          localResult.mCountOfPoints[4] = json.optInt("point5Count");
          //localResult.mHasBought = json.optBoolean("hasBoughtApp", false);
          ArrayList<ArticleInfo> articleList = ArticleInfo.valueOf(json);
          if (articleList != null)
          {
            localResult.mArticleList = articleList;
          }
		}

		return localResult;
			
	}
	
	
	protected boolean dataExists()
	  {
		Log.v("dataExists");
	    if (super.dataExists())
	      if (mPage == 1 ||((((Result)this.mResult).mArticleList != null) && ((ArrayList<ArticleInfo>)((MainPageLoader.Result)this.mResult).mArticleList).size() > 0) )
	      {
	    		Log.v("dataExists:"  + ((ArrayList<ArticleInfo>)((MainPageLoader.Result)mResult).mArticleList).size());		    	
	    	  return true;
	      }
	      
		return false;
	       
	  }
	
	
	@Override
	protected BaseLoader.UpdateTask getUpdateTask() {
		// TODO Auto-generated method stub
		return new AppCommentUpdateTask();
	}
	
	
	private class AppCommentUpdateTask extends BaseLoader.UpdateTask
    {
		protected boolean mIsAppend;
        private AppCommentUpdateTask()
        {
            super();
            if (MainPageLoader.this.mPage == 1)
            	this.mIsAppend =  false;
            else
            	this.mIsAppend =  true;
        }

		@Override
		protected Request getRequest() {
			Log.v(String.format(Constant.GET_ARTICLES_ON_MAIN,MainPageLoader.this.mAppId,mPage));
			return new Request(String.format(Constant.GET_ARTICLES_ON_MAIN,MainPageLoader.this.mAppId,mPage));
		}
		
		
		private MainPageLoader.Result merge(MainPageLoader.Result oldresult, MainPageLoader.Result newresult)
	    {
	      MainPageLoader.Result localResult = new MainPageLoader.Result();
	      localResult.mCommentTotalCount = newresult.mCommentTotalCount;
	      localResult.mScoreTotalCount = newresult.mScoreTotalCount;
	      localResult.mScore = newresult.mScore;
	      localResult.mArticleList = new ArrayList<ArticleInfo>();
	      Log.v("merge 1:"  + ((ArrayList<ArticleInfo>)((MainPageLoader.Result)mResult).mArticleList).size());
	        if (oldresult != null)
	          localResult.mArticleList.addAll(oldresult.mArticleList);
	        if (newresult != null)
	           localResult.mArticleList.addAll(newresult.mArticleList);
	        
	        Log.v("merge 2:"  + ((ArrayList<ArticleInfo>)((MainPageLoader.Result)mResult).mArticleList).size());
	        
		  return localResult;
	        
	    }

	    protected BaseResult onDataLoaded(BaseResult oldresult, BaseResult newresult)
	    {
	    	BaseResult localResult = null;
	    	
	    	if(newresult != null && (((Result)newresult).mArticleList != null) && (!((Result)newresult).mArticleList.isEmpty()))
	    	{
	    		MainPageLoader.this.mNeedNextPage = true;
	    		localResult = newresult;
	    		
	    		mResult = newresult.shallowClone();
	    		Log.v("this.mIsAppend:" + this.mIsAppend);
	    		if(this.mIsAppend )
		    	{
		    		localResult = merge((MainPageLoader.Result)oldresult, (MainPageLoader.Result)newresult);
		    	}
	
	    		return localResult;
	    	}
	    	
	    	
	    	return newresult;

	    }
           
    }
	
	public static final class Result extends BaseResult
    {
        public ArrayList<ArticleInfo> mArticleList;
        public int mCommentTotalCount;
        public int[] mCountOfPoints;
        public boolean mHasBought = false;
        public double mScore;
        public int mScoreTotalCount;
        
        protected int getCount()
        {
        	if(this.mArticleList != null)
        		return 1;
        	return 0;
        }
        
        public BaseResult shallowClone()
        {
            Result result = new Result();
            result.mArticleList = this.mArticleList;
            result.mCommentTotalCount = this.mCommentTotalCount;
            result.mScoreTotalCount = this.mScoreTotalCount;
            result.mScore = this.mScore;
            result.mCountOfPoints = this.mCountOfPoints;
            result.mHasBought = this.mHasBought;
            return result;
        }
    }
	
	public void reload()
	  {
	    if (!isLoading())
	    {
	      this.mPage = 0;
	      this.mNeedNextPage = false;
	      super.reload();
	    }
	  }
	
	public void nextPage() {
		if (this.mNeedNextPage)
		      this.mPage = (1 + this.mPage);
		
	}

	public void setAppId(String mAppId2) {
		mAppId = mAppId2;
		
	}

	
}

